package cn.xspace.library.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.xspace.library.R;
import cn.xspace.library.bean.OrgDeptUserBean;
import cn.xspace.library.view.tree.bean.Node;

/**
 * Created by liuchen on 2017-02-09.
 */

public class AddressbookViewHolder extends SubViewHolder<Node> {
    ImageView iconLeft;
    TextView label;
    ImageView iconRight;
    RelativeLayout rlRight;


    TreeRecycleViewAdapter.IItemClickListener iItemClickListener;
    TreeRecycleViewAdapter.IParentClickListener iParentClickListener;

    public void setChangeIcon(boolean changeIcon) {
        isChangeIcon = changeIcon;
    }

    private boolean isChangeIcon = false;

    public AddressbookViewHolder(View view) {
        super(view);
        convertView = view;
        this.iconLeft = (ImageView) view.findViewById(R.id.img_org_left);
        this.label = (TextView) view.findViewById(R.id.txt_name_org);
        this.iconRight = (ImageView) view.findViewById(R.id.img_org_right);
        this.rlRight = (RelativeLayout) view.findViewById(R.id.rl_org_right);
    }

    public void setiItemClickListener(TreeRecycleViewAdapter.IItemClickListener iItemClickListener) {
        this.iItemClickListener = iItemClickListener;
    }

    private int getPadLeft(int level) {
        if (level == 1) {
            return 50;
        } else if (level > 1) {
            return 50 + (level - 1) * 20;
        }
        return 20;
    }

    @Override
    public void bindHolder(Node node) {

        // 设置内边距
        int padleft = getPadLeft(node.getLevel());
        ViewGroup vg = (ViewGroup) convertView;
        View oneView = vg.getChildAt(0);
        if (oneView instanceof RelativeLayout) {
        } else if (oneView instanceof ImageView) {
        }
        if (oneView != null) {
//            oneView.setPadding(padleft, 0, 10, 0);
            int top = convertView.getPaddingTop();
            int bottom = convertView.getPaddingBottom();
//            oneView.setPadding(padleft + 10, top, 20, bottom);
            convertView.setPadding(padleft + 10, top, 20, bottom);
        } else {
            convertView.setPadding(padleft, 3, 3, 3);
        }

        final OrgDeptUserBean bean = (OrgDeptUserBean) node.getObject();
        this.label.setText(bean.dept_name);
//            this.iconRight.setImageResource(node.getIcon());
        if (isChangeIcon) {
            if (node.isExpand()) { //展开
                iconLeft.setImageResource(R.drawable.green_triangle_down);
            } else {
                iconLeft.setImageResource(R.drawable.green_triangle_right);
            }
        } else {
            setParentIcon(node.isRoot());
        }
        if (!bean.isLookOrg) { //是否是查看机构群组
            this.rlRight.setVisibility(View.GONE);
        }
    }

    public void setParentIcon(boolean isRoot) {
        if (isRoot) { //如果是根节点
            iconLeft.setImageResource(R.drawable.icon_top_logo);
        } else {
            iconLeft.setImageResource(R.drawable.icon_org_left);
        }
    }
}
